﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BaseView.View
{
    /// <summary>
    /// LogInView.xaml 的交互逻辑
    /// </summary>
    public partial class LogInView : Window
    {
        private MesTool.Model.UserModel _model;
        private MesTool.UserBLL userBLL =null;
        /// <summary>
        /// 登录界面
        /// </summary>
        /// <param name="model">人员信息</param>
        /// <param name="ids">历史账号</param>
        public LogInView(MesTool.Model.UserModel model, List<string> ids = null)
        {
            try
            {
                HandyControl.Tools.ConfigHelper.Instance.SetLang("zh-CN");
                InitializeComponent();
                _model = model;
                if (ids != null && ids.Count != 0)
                {
                    ComboBoxId.ItemsSource = ids;
                    ComboBoxId.SelectedIndex = 0;
                }
                userBLL = new MesTool.UserBLL("192.168.2.2");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //登录
                if (ComboBoxId.Text == "")
                {
                    HandyControl.Controls.MessageBox.Warning("请输入账号！", "提示");
                    return;
                }
                // 使用.NET内部算法把IntPtr指向处的字符集合转换成字符串 
                IntPtr p = Marshal.SecureStringToBSTR(this.PasswordBoxPass.SecurePassword);
                // 顺便校验一下
                string password = Marshal.PtrToStringBSTR(p);
                if (password == "")
                {
                    HandyControl.Controls.MessageBox.Warning("请输入密码！", "提示");
                    return;
                }
                //Mes登录
                //todo 可切换其他登录
                //bool login = userBLL.Login(ComboBoxId.Text.ToLower(), password);
                //跳过Mes
                if (true)
                {
                    //ERP
                    //var model = userBLL.GetErpUserModel(ComboBoxId.Text.ToLower());
                    //DingTalk
                    var model = userBLL.GetDingTalkUserModel(ComboBoxId.Text.ToLower());
                    string err = "获取姓名失败";
                    //var model = MesTool.UserBLL.GetUserName(ComboBoxId.Text.ToLower(),ref err);
                    if (model == null)
                    {
                        HandyControl.Controls.MessageBox.Warning(err, "提示");
                        return;
                    }
                    _model.userName = model.userName;
                    _model.nickName = model.nickName;
                    _model.bumen = model.bumen;
                    _model.zhiwei = model.zhiwei;
                    //写日志
                    BaseTool.LogHelper.WriteDebug("账号登录：" + model.nickName);
                    //返回True提供判断
                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    HandyControl.Controls.MessageBox.Warning("账号或密码错误！", "提示");
                    return;
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message, "错误");
                BaseTool.LogHelper.WriteError("登录失败", ex);
                return;
            }
        }

        private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                //回车键
                if (e.Key == Key.Enter)
                {
                    Button_Click(null, null);
                }
                //Esc键
                else if (e.Key == Key.Escape)
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Error(ex.Message, "错误");
                BaseTool.LogHelper.WriteError("登录失败", ex);
                return;
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBoxId.Focus();
            //动画
            DoubleAnimation opacityAnimation = new DoubleAnimation();
            opacityAnimation.From = 0.1;
            opacityAnimation.To = 1;
            opacityAnimation.Duration = TimeSpan.FromSeconds(0.5);
            StackPanelInfo.BeginAnimation(StackPanel.OpacityProperty,opacityAnimation);
        }


    }
}
